#include<stdio.h>
#include<stdlib.h>
typedef struct number
{
    int data;
    struct number *ptr;
}num;
num*  addnum(num *head,int adddata )
{
    num *current=head;
    num *add=(num*)malloc(sizeof(num));
    if(head==NULL)
    {
        head=add;
    }
   else
   {
        while(current->ptr!=NULL)
        {
            current=current->ptr;
        }
        current->ptr=add;
    }
    add->data=adddata;
    add->ptr=NULL;
    return head;
}
void shownuma(num* head)
{
    num* current=head;
    if(head!=NULL)
    {
        printf("The new list A:");
    while(current->ptr!=NULL)
    {
        printf("%d ",current->data);
        current=current->ptr;
    }
    printf("%d\n",current->data);
    }
    else{
        printf("There is no item in A list.\n");
    }
}
void shownumb(num* head)
{
    num* current=head;
    if(head!=NULL)
    {
        printf("The new list B:");
    while(current->ptr!=NULL)
    {
        printf("%d ",current->data);
        current=current->ptr;
    }
    printf("%d\n",current->data);
    }
    else{
        printf("There is no item in B list.\n");
    }
}

void delenum(num *head)
{
    num*current=head;
    while(current->ptr!=NULL)
    {
        current=current->ptr;
        free(head);
        head=current;
    }
}

void guibing(num* heada,num* headb)
{
    if(heada==NULL&&headb==NULL)
    {
        ;
    }
    else if(heada==NULL&&headb!=NULL)
    {
        num* tem=heada;
        heada=headb;
        headb=tem;
    }
    else if(heada!=NULL&&headb==NULL)
    {
        ;
    }
    else{
        num *virtual_heada=(num*)malloc(sizeof(num));
        num *virtual_headb=(num*)malloc(sizeof(num));
        virtual_heada->data=0;
        virtual_headb->data=0;
        virtual_heada->ptr=heada;
        virtual_headb->ptr=headb;
        num* preva=virtual_heada;
        num* prevb=virtual_headb;
        num *firsta=preva->ptr;
        num *firstb=prevb->ptr;
        num *temp;
        while(firstb!=NULL)
        {
            if(!firsta)
            {
                preva->ptr=firstb;
                prevb->ptr=NULL;
                break;
            }
            
            else if(firsta->data==firstb->data)
            {
                preva=firsta;
                firsta=firsta->ptr;
                prevb=firstb;
                firstb=firstb->ptr;
            }
            else if(firstb->data<firsta->data)
            {
               temp=firstb->ptr;
               preva->ptr=firstb;
               preva=firstb;
               preva->ptr=firsta;
               prevb->ptr=temp;
               firstb=temp;
               
            }else if(firsta->data<firstb->data)
            {
                preva=firsta;
                firsta=firsta->ptr;
            }
            // shownuma(virtual_heada);
            // shownumb(virtual_headb);
            // printf("%d",preva->data);
            // if(firsta!=NULL) printf("%d",firsta->data);
            // printf("\n");
        }
        heada=virtual_heada->ptr;
        headb=virtual_headb->ptr;
    }
    
    
    
    
    shownuma(heada);
    shownumb(headb);
    delenum(heada);
    delenum(headb);

}



int main()
{
    int a;
    int m,n,x,y;
    num* heada=NULL,*headb=NULL;
    while(1)
    {
        scanf("%d",&a);
        if(a==-1)
        {
            break;
        }
        else{
           heada=addnum(heada,a);
        }
    }

     while(1)
    {
        scanf("%d",&a);
        if(a==-1)
        {
            break;
        }
        else{
           headb=addnum(headb,a);
        }
    }
    
    
    
    
    
    
    guibing(heada,headb);
    

      
   
   
    
    
    return 0;
}